/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package genetic;

import java.util.BitSet;
import reader.Header;


/**
 *
 * @author Ifun
 */
public final class Chromosome implements Comparable<Chromosome> {
    BitSet container;
    Integer nfit;
    String content;
    int maxbit;
    
    public Chromosome()
    {
        
    }
    
    public Chromosome(Header listAtribut)
    {
        //generateRandomChrom(listAtribut);
        maxbit = listAtribut.getMaxBit();
        content = generateRandomChrom(listAtribut);
        
    }
    
    public int compareTo(Chromosome friend)
    {
        if(this.nfit == friend.nfit)
            return 0;
        else if(this.nfit > friend.nfit)
            return -1;
        else
            return 1;
    }
    
    public String getContent()
    {
        return content;
    }
    
    public void setNFit(int num)
    {
        nfit = num;
    }
    
    public  Chromosome[] crossOverChrom(Chromosome Ayah, Chromosome Ibu){
        
        String konten1 = Ayah.content;
        String konten2 = Ibu.content;
        
        int mb = maxbit;
        int idx = genMath.getRandomInt(konten1.length()-mb);
        
        String subkontenprev1 = konten1.substring(0, idx);
        String subkontenprev2 = konten2.substring(0, idx);
        String subkontennext1 = konten1.substring(idx, konten1.length());
        String subkontennext2 = konten2.substring(idx, konten2.length());
        
        Chromosome anak1 = new Chromosome();
        Chromosome anak2 = new Chromosome();
        anak1.content=subkontenprev1.concat(subkontennext2);
        anak2.content=subkontenprev2.concat(subkontennext1);
        
        Chromosome[] anak = new Chromosome[2];
        anak[0]=anak1;
        anak[1]=anak2;
        return anak;
    }
    
    public void mutasiChrom(){
        String konten = this.content;
        int mb = maxbit;
        int idx = genMath.getRandomInt((konten.length()-mb));
        
        char dna = konten.charAt(idx);
        if(dna == '0'){
            StringBuilder mut = new StringBuilder(konten);
            mut.setCharAt(idx, '1');
            konten=mut.toString();
        }else{
            StringBuilder mut = new StringBuilder(konten);
            mut.setCharAt(idx, '0');
            konten=mut.toString();
        }
        this.content=konten;
    }
    
    public String generateRandomChrom(Header Attrib){
        
        int maxvalue = Attrib.getMaxJumlahValue();
        int maxattrib = Attrib.getNumAttribute();
        int mb = Attrib.getMaxBit();
        
        int[] attribs = new int[maxattrib+1];
        
        String janin = new String();
        
        genMath gen = new genMath();

        for(int i=0;i<maxattrib + 1;i++){
            attribs[i] = genMath.getRandomInt(maxvalue);
        }
        for(int i=0;i<attribs.length;i++){
            String zigot = Integer.toBinaryString(attribs[i]);
            janin = janin.concat(String.format("%"+mb+"s",zigot).replace(' ', '0'));
        }
        //Chromosome anak = new Chromosome();
        //anak.content=janin;
        return janin;
    }
    
    public void printChromosome()
    {
        System.out.println("chromosome: " + content);
    }
    
    public int getNCovered()
    {
        return nfit;
    }
}
